package com.ddone.common.result;

import lombok.Data;
import lombok.Getter;

/**
 * @author ddone
 * @date 2024/4/11-13:25
 */
@Data
public class Response<T> {
    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Response() {
    }

    // 返回数据
    protected static <T> Response<T> build(T data) {
        Response<T> result = new Response<T>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> Response<T> build(T body, Integer code, String message) {
        Response<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Response<T> build(T body, ResponseEnum responseEnum) {
        Response<T> result = build(body);
        result.setCode(responseEnum.getCode());
        result.setMessage(responseEnum.getMessage());
        return result;
    }

    public static <T> Response<T> ok() {
        return Response.ok(null);
    }

    /**
     * 操作成功
     *
     * @param data baseCategory1List
     * @param <T> 返回的结果
     * @return
     */
    public static <T> Response<T> ok(T data) {
        Response<T> result = build(data);
        return build(data, ResponseEnum.SUCCESS);
    }

    public static <T> Response<T> fail() {
        return Response.fail(null);
    }

    /**
     * 操作失败
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail(T data) {
        Response<T> result = build(data);
        return build(data, ResponseEnum.ERROR);
    }

    public Response<T> message(String msg) {
        this.setMessage(msg);
        return this;
    }

    public Response<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}
